package approve.action;

import java.util.ArrayList;
import java.util.List;

import nguyen.dao.ReceiverDocumentDAO;

import model.pojo.Document;
import model.pojo.Employee;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;

public class Re_ApprovedDocument extends Activity {
	String database = "admin";
	Bundle language=null;
	String lang = "EN";
	public String getDatabase() {
		Bundle extras = getIntent().getExtras();
		if (extras != null) {
			database = extras.getString("database");
			lang=extras.getString("lang");
			language=extras.getBundle(lang);
		}
		return database;
	}
	
	Employee emp = null;

	ListView list;
	List map = null;
	List<Document> listItem = new ArrayList<Document>();
	int count = 0;
	LinearLayout listPages;
	TextView txt_first, txt_last,txt_prev,txt_next;
	ImageView img_next, img_prev;
	int pageSize = 6;
	int pageCount = 0;
	int maxPages = 2;
	int currentPage = 1;
	List<Integer> listCurrentPages = new ArrayList<Integer>();

	@Override
	public void onCreate(final Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.re_approveddocument);
		getDatabase();
		
		emp = (Employee) getIntent().getExtras().get("em");
		list = (ListView) findViewById(R.id.approveddocumentlist);
		txt_prev=(TextView) findViewById(R.id.txt_prev);txt_prev.setText("<<");
		txt_next=(TextView) findViewById(R.id.txt_next);txt_next.setText(">>");

		map = getListApprovedDocument(pageSize, currentPage);
		if (map != null && map.size() > 1) {
			listItem = (List<Document>) map.get(0);
			count = (Integer) map.get(1);
		}
		if(language!=null){
        	TextView la_headerTitle=(TextView)findViewById(R.id.la_headerTitle);
        	la_headerTitle.setText(language.getString("approved-document")+" ("+count+"):");
        }
		
		list.setAdapter(new ArrayAdapter<Document>(this,
				android.R.layout.simple_list_item_1, listItem));
		list.setOnItemClickListener(viewDocument);

		// paging
		pageCount = (int) Math.ceil((double) count / pageSize);
		

		if (pageCount > 0) {
			txt_first = (TextView) findViewById(R.id.txt_first);
			if (currentPage != 1) {
				txt_first.setTextColor(Color.BLACK);
				txt_first.setOnClickListener(new View.OnClickListener() {
					// @Override
					public void onClick(View v) {
						currentPage = 1;
						onCreate(savedInstanceState);

					}
				});
				txt_first.setOnTouchListener(setTouchColor);
			}
			txt_last = (TextView) findViewById(R.id.txt_last);
			if (currentPage != pageCount) {
				txt_last.setTextColor(Color.BLACK);
				txt_last.setOnClickListener(new View.OnClickListener() {
					// @Override
					public void onClick(View v) {
						currentPage = pageCount;
						onCreate(savedInstanceState);

					}
				});
				txt_last.setOnTouchListener(setTouchColor);
			}
			
			listPages = (LinearLayout) findViewById(R.id.listpages);
			listCurrentPages.clear();
			final int secIndex = (int) Math.ceil((double) currentPage
					/ maxPages);
			//image next prev
			if (currentPage > maxPages) {				
				txt_prev.setTextColor(Color.BLACK);
				txt_prev.setOnClickListener(new View.OnClickListener() {
					// @Override
					public void onClick(View v) {
						int secIndexTemp = secIndex - 1;
						int minIndexTemp = (secIndexTemp * maxPages)
								- (maxPages - 1);
						currentPage = minIndexTemp;
						onCreate(savedInstanceState);
					}
				});
				txt_prev.setOnTouchListener(setTouchColor);
//				img_prev.setImageResource(R.drawable.prev);
//				
//				img_prev.setOnClickListener(new View.OnClickListener() {
//					// @Override
//					public void onClick(View v) {
//						int secIndexTemp = secIndex - 1;
//						int minIndexTemp = (secIndexTemp * maxPages)
//								- (maxPages - 1);
//						currentPage = minIndexTemp;
//						onCreate(savedInstanceState);
//					}
//				});
			}

			if (currentPage < pageCount) {
				txt_next.setTextColor(Color.BLACK);
				
				txt_next.setOnClickListener(new View.OnClickListener() {
					// @Override
					public void onClick(View v) {
						int secIndexTemp = secIndex + 1;
						int minIndexTemp = (secIndexTemp * maxPages)
								- (maxPages - 1);
						currentPage = minIndexTemp;
						onCreate(savedInstanceState);
					}
				});
				txt_next.setOnTouchListener(setTouchColor);
//				img_next.setImageResource(R.drawable.next);
//				
//				img_next.setOnClickListener(new View.OnClickListener() {
//					// @Override
//					public void onClick(View v) {
//						int secIndexTemp = secIndex + 1;
//						int minIndexTemp = (secIndexTemp * maxPages)
//								- (maxPages - 1);
//						currentPage = minIndexTemp;
//						onCreate(savedInstanceState);
//					}
//				});
			}
			//list pages
			int maxIndex = secIndex * maxPages;
			int minIndex = maxIndex - (maxPages - 1);
			if (maxIndex <= pageCount) {
				TextView space1=new TextView(this);
				space1.setWidth(5);
				listPages.addView(space1);
				for (int i = minIndex; i <= maxIndex; i++) {
					final int index = i;
					TextView tv = new TextView(this);
					tv.setText(i + "");
					tv.setTextColor(0xFF7F7F7F);
					if (currentPage != i) {
						tv.setTextColor(Color.BLACK);
						tv.setOnClickListener(new View.OnClickListener() {
							// @Override
							public void onClick(View v) {
								currentPage = index;
								onCreate(savedInstanceState);
							}
						});
					}					
					tv.setOnTouchListener(setTouchColor);
					tv.setPadding(15, 10, 15, 10);					
					listPages.addView(tv);
					TextView space=new TextView(this);
					space.setWidth(5);
					listPages.addView(space);
					listCurrentPages.add(i);
				}
			} else {
				TextView space1=new TextView(this);
				space1.setWidth(5);
				listPages.addView(space1);
				for (int i = minIndex; i <= pageCount; i++) {
					final int index = i;
					TextView tv = new TextView(this);
					tv.setText(i + "");
					tv.setTextColor(0xFF7F7F7F);
					if (currentPage != i) {
						tv.setTextColor(Color.BLACK);
						tv.setOnClickListener(new View.OnClickListener() {
							// @Override
							public void onClick(View v) {
								currentPage = index;
								onCreate(savedInstanceState);
							}
						});
					}
					tv.setOnTouchListener(setTouchColor);
					tv.setPadding(15, 8, 15, 8);
					listPages.addView(tv);
					TextView space=new TextView(this);
					space.setWidth(5);
					listPages.addView(space);
					listCurrentPages.add(i);
				}
			}
		}else{
			if(listItem.size()<1){
				TextView empltyList=(TextView) findViewById(R.id.empltyList);
				Bundle language=getIntent().getExtras().getBundle(getIntent().getExtras().getString("lang"));
				if(language!=null){
					empltyList.setText(language.getString("no_data"));
				}				
			}
		}

		// listPages.setAdapter(new ArrayAdapter<Integer>(this,
		// android.R.layout.simple_list_item_1,listCurrentPages));

	}
	private OnTouchListener setTouchColor=new OnTouchListener() {
		
		@Override
		public boolean onTouch(View view, MotionEvent event) {
			switch(event.getAction()) {
			case MotionEvent.ACTION_DOWN:
				view.setBackgroundColor(0xFF7F7F7F);
				break;
			case MotionEvent.ACTION_UP:
				view.setBackgroundColor(0xFFBEBEBE);
				break;
			default:
				break;
			}
			return false;
		}
	};
	
	private OnItemClickListener viewDocument = new OnItemClickListener() {

		public void onItemClick(AdapterView<?> listDoc, View arg1, int arg2,
				long arg3) {
			Document doc = (Document) listDoc.getItemAtPosition(arg2);
			if (doc.getTypeDoc() < 11) {
				Intent intent = new Intent(Re_ApprovedDocument.this,
						Re_ViewForm_LessThan10.class);
				intent.putExtra("doc", doc);
				intent.putExtras(getIntent().getExtras());
				startActivity(intent);
			} else if (doc.getTypeDoc() == 12) {
				Intent intent = new Intent(Re_ApprovedDocument.this,
						Re_ViewForm12.class);
				intent.putExtra("doc", doc);
				intent.putExtras(getIntent().getExtras());
				startActivity(intent);
			} else if (doc.getTypeDoc() == 13) {
				Intent intent = new Intent(Re_ApprovedDocument.this,
						Re_ViewForm13.class);
				intent.putExtra("doc", doc);
				intent.putExtras(getIntent().getExtras());
				startActivity(intent);
			}
			else if (doc.getTypeDoc() == 16) {
				Intent intent = new Intent(Re_ApprovedDocument.this,
						Re_ViewForm16.class);
				intent.putExtra("doc", doc);
				intent.putExtras(getIntent().getExtras());
				startActivity(intent);
			}else if (doc.getTypeDoc() == 15) {
				Intent intent = new Intent(Re_ApprovedDocument.this,
						Re_ViewForm15.class);
				intent.putExtra("doc", doc);
				intent.putExtras(getIntent().getExtras());
				startActivity(intent);
			} else {
				Intent intent = new Intent(Re_ApprovedDocument.this,
						Re_ViewForm14.class);
				intent.putExtra("doc", doc);
				intent.putExtras(getIntent().getExtras());
				startActivity(intent);
			}

		}

	};

	// private List<Integer> getListPaging() {
	// pageCount=list.getAdapter().getCount()/pageSize;
	//
	// List<Integer> listPaging=new ArrayList<Integer>();
	//
	// for (int i = 1; i <= pageCount; i++) {
	// listPaging.add(i);
	// }
	// return listPaging;
	// }

	public List getListApprovedDocument(int limit, int currentPage) {
		return ReceiverDocumentDAO.getApprovedDocument(emp.getIdemployee(),
				database, limit, currentPage,lang);
	}

	// public List<Document> getListApprovedDocument(){
	// String host="14.63.219.99";
	// String database="elihan";
	// String url="jdbc:mysql://"+host+":3306/"+database;
	// String user="erp";
	// String password="nguyen";
	// String
	// sql="SELECT approvedoc.* FROM approvedoc,listapprove WHERE  IDTypeDoc != 11 and listapprove.IDEmployee='1322887525530'"
	// +
	// " AND listapprove.IDGroupApprove = approvedoc.IDGroupApprove AND listapprove.IDStatus=1 "
	// +
	// " AND approvedoc.IDStatus=1";
	// Connection con=null;
	//
	// List<Document> list=new ArrayList<Document>();
	// try {
	// DriverManager.registerDriver(new org.gjt.mm.mysql.Driver());
	// con=(Connection) DriverManager.getConnection(url, user, password);
	// Statement statement= (Statement) con.createStatement();
	// ResultSet rs= statement.executeQuery(sql);
	// Document doc=null;
	// while(rs.next()){
	// doc=new
	// Document(rs.getInt("IDApproveDoc"),rs.getString("Tile"),rs.getString("Description"),rs.getString("Content"));
	// list.add(doc);
	// }
	// } catch (SQLException e) {
	// openDialog();
	// System.out.println("ERROR CONNECT");
	// e.printStackTrace();
	// }
	// finally{
	// try {
	// con.close();
	// } catch (SQLException e) {
	// e.printStackTrace();
	// }
	// }
	//
	// return list;
	//
	// }

	public void openDialog() {
		AlertDialog.Builder builder = new AlertDialog.Builder(this);
		builder.setTitle("Ok");// Set tiÃªu Ä‘á»�

		builder.setCancelable(false);// Set cÃ³ cho ngÆ°á»�i dÃ¹ng
										// Cancer báº±ng nÃºt
										// quay láº¡i (back) ko? false: ko
		builder.setPositiveButton("Yes", new DialogInterface.OnClickListener() {

			public void onClick(DialogInterface dialog, int which) {
				// LÃ m cÃ¡i gÃ¬ Ä‘Ã³ khi áº¥n Yes táº¡i
				// Ä‘Ã¢y
				dialog.dismiss();
			}
		});

		builder.show();
	}
}
